import sys

from PyQt5.QtGui import QPalette
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QLabel, QPushButton, QHBoxLayout, QLineEdit, QApplication, QWidget, QColorDialog, QFileDialog


class InputDialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("InputDialog")

        # 创建布局
        self.layout = QVBoxLayout()
        self.setLayout(self.layout)

        # 添加组件
        self.label = QLabel("Enter your name:")
        self.line_edit = QLineEdit()
        self.ok_button = QPushButton("OK")
        self.cancel_button = QPushButton("Cancel")

        # 按钮布局
        button_layout = QHBoxLayout()
        button_layout.addWidget(self.ok_button)
        button_layout.addWidget(self.cancel_button)

        # 添加组件到主布局
        self.layout.addWidget(self.label)
        self.layout.addWidget(self.line_edit)
        self.layout.addLayout(button_layout)

        # 按钮事件绑定
        self.ok_button.clicked.connect(self.accept)
        self.cancel_button.clicked.connect(self.reject)

    def get_input_text(self):
        return self.line_edit.text()

    def accept(self) -> None:
        print(self.line_edit.text())
        self.close()


class ColorDialog(QDialog):
    def __init__(self):
        super().__init__()

        layout = QVBoxLayout()
        self.setLayout(layout)

        self.color_label = QLabel("Hello, World!")
        layout.addWidget(self.color_label)

        font_color_button = QPushButton("选择字体颜色")
        font_color_button.clicked.connect(self.choose_font_color)
        layout.addWidget(font_color_button)

        bg_color_button = QPushButton("选择背景颜色")
        bg_color_button.clicked.connect(self.choose_bg_color)
        layout.addWidget(bg_color_button)

    def choose_font_color(self):
        color = QColorDialog.getColor()
        if color.isValid():
            # 设置字体颜色
            p = QPalette()
            p.setColor(QPalette.WindowText, color)
            self.color_label.setPalette(p)

    def choose_bg_color(self):
        color = QColorDialog.getColor()
        if color.isValid():
            # 获取当前标签的字体颜色,防止设置背景时将字体设置成默认颜色
            font_color = self.color_label.palette().color(self.color_label.foregroundRole())

            p = QPalette()
            p.setColor(QPalette.Window, color)
            p.setColor(QPalette.WindowText, font_color)
            self.color_label.setAutoFillBackground(True)
            self.color_label.setPalette(p)


class MainPage(QWidget):
    def __init__(self):
        super().__init__()
        hlaout = QHBoxLayout()
        btn1 = QPushButton("输入弹出框")
        btn1.clicked.connect(self.showDialog)
        hlaout.addWidget(btn1)

        btn2 = QPushButton("颜色弹出框")
        btn2.clicked.connect(self.showColorDialog)
        hlaout.addWidget(btn2)

        btn3 = QPushButton("文件弹出框")
        btn3.clicked.connect(self.showFileDialog)
        hlaout.addWidget(btn3)

        self.setLayout(hlaout)
        self.setFixedWidth(500)
        self.setFixedHeight(300)

    def showDialog(self):
        inputDialog = InputDialog()
        inputDialog.exec_()

    def showColorDialog(self):
        colorDialog = ColorDialog()
        colorDialog.exec_()

    def showFileDialog(self):
        fname = QFileDialog.getOpenFileNames(self, 'Open file', '', 'Text files (*.txt)')
        if fname[0]:
            print(fname[0])  # 打印选定的文件路径


if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainPage = MainPage()
    mainPage.show()
    sys.exit(app.exec_())
